package com.zcxy360.sandtable.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.Problem;
import com.zcxy360.sandtable.domain.ProblemExample;
import com.zcxy360.sandtable.domain.ProblemExample.Criteria;
import com.zcxy360.sandtable.domain.ProblemMapper;
import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.domain.VproblemMapper;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;

@Transactional
@Service
public class ProblemService {

	@Autowired
	private ProblemMapper problemMapper;
	@Autowired
	private VproblemMapper vproblemMapper;

	public Object getByProblemId(Integer problemId,Integer deep) {
		Problem problem = null;
		switch (deep) {
		case 0:
			problem = problemMapper.selectByPrimaryKey(problemId);
			break;
		case 1:
			problem = problemMapper.selectByPrimaryKeyWithExtra(problemId);
			break;
		default:
			break;
		}
		return problem;
	}

	public Object list(Problem problem, String key,String orderByClause,Integer sign, Integer pageNum, Integer pageSize, Integer navigatePages) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			problem.setOrgId(loginUser.getOrgId());
		}
		if(sign != null) {
			//只看自己
			if(sign == 1 && !loginRole.equals("admin")) {
				problem.setUserId(loginUser.getUserId());
			}
			//看非自己的其他老师资源
			if(sign == 2) {
				problem.setUserId(loginUser.getUserId());
			}
			//看内建资源
			if(sign == 3) {
				problem.setUserId(1);
				problem.setOrgId(null);
			}
			//看本机构自己的所有资源及其它老师共享资源及内建资源
			if(sign == 4) {
				problem.setUserId(loginUser.getUserId());
			}
			//看本机构所有资源及内建资源
			if(sign == 5) {
			}
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Problem> problems = problemMapper.listWithExtra(problem,StringUtil.wrapper(key),orderByClause,sign);
		return new PageInfo<>(problems, navigatePages);
	}

	public Problem post(Problem problem) {
		User loginUser = ShiroUtil.getUser();
		problem.setUserId(loginUser.getUserId());
		problem.setOrgId(loginUser.getOrgId());
		if(problem.getType() == 1) {
			problem.setRoughTime(10);
		}else {
			problem.setRoughTime(2);
		}
		problemMapper.insertSelective(problem);
		vproblemMapper.insertByProblemId(problem.getProblemId());
		return problem;
	}

	public boolean update(Problem problem) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			Problem p = problemMapper.selectByPrimaryKey(problem.getProblemId());
			if(loginRole.equals("teacher")) {
				if(!p.getUserId().equals(loginUser.getUserId())) {
					throw new RuntimeException("老师只能修改本人的资源");
				}
			}
			if(loginRole.equals("manager")) {
				if(!p.getOrgId().equals(loginUser.getOrgId())) {
					throw new RuntimeException("机构管理员只能修改本机构的资源");
				}
			}
		}
		if(problem.getType() != null) {
			if(problem.getType() == 1) {
				problem.setRoughTime(10);
			}else {
				problem.setRoughTime(2);
			}
		}
		problemMapper.updateByPrimaryKeySelective(problem);
		vproblemMapper.insertByProblemId(problem.getProblemId());
		return true;
	}
	
	public boolean delete(int[] problemIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		Problem p;
		if(!loginRole.equals("admin")) {
			for(int problemId:problemIds) {
				p = problemMapper.selectByPrimaryKey(problemId);
				if(loginRole.equals("teacher")) {
					if(!p.getUserId().equals(loginUser.getUserId())) {
						throw new RuntimeException("老师只能删除本人的资源");
					}
				}
				if(loginRole.equals("manager")) {
					if(!p.getOrgId().equals(loginUser.getOrgId())) {
						throw new RuntimeException("机构管理员只能删除本机构的资源");
					}
				}
			}
		}
		problemMapper.deleteBatch(problemIds) ;
		return true;
	}

	public boolean checkName(String name) {
		User loginUser = ShiroUtil.getUser();
		ProblemExample ex = new ProblemExample();
		Criteria or = ex.or();
		or.andNameEqualTo(name);
		if(loginUser.getRole().equals("admin")) {
			or.andOrgIdIsNull();
		}else {
			or.andOrgIdEqualTo(loginUser.getOrgId());
		}
		return problemMapper.countByExample(ex) == 0;
	}

}
